//
//  ViewController.m
//  09-nsurlsession06-文件上传
//
//  Created by J on 16/2/26.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDataDelegate>
@property(nonatomic,strong)NSURLSession* session;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
    config.timeoutIntervalForRequest = 10;
    config.allowsCellularAccess = NO;//是否允许非Wifi传输
        NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
    self.session = session;
//    NSURLSession* session = [NSURLSession sharedSession];
    //POST请求将文件数据放到请求体里面
    //参数名-参数值
    NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]];
    req.HTTPMethod = @"POST";
    
    // 设置请求头(告诉服务器,这是一个文件上传的请求)
    [req setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", @""] forHTTPHeaderField:@"Content-Type"];
    
    
    
    //fromData 请求体数据  文件数据req.httpBody会被忽略 The body stream and body data in this request object are ignored.
    [self.session uploadTaskWithRequest:req fromData:nil completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
    }];//NSURLSessionDataDelegate上传监听协议
    
//    这个用于PUT请求
//    session uploadTaskWithRequest:<#(nonnull NSURLRequest *)#> fromFile:<#(nonnull NSURL *)#>
    
  
    
}

@end
